package com.bookcode.config;


import com.bookcode.entity.Coffee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration      //配置类
public class CoffeeConfiguration {
    @Bean
    ReactiveRedisOperations<String, Coffee> redisOperations(ReactiveRedisConnectionFactory factory) {
//Redis没有表结构的概念，需要使用JSON格式的文本作为Redis和Java普通对象互相交换数据的存储格式
        //RedisTemplate初始化
//为了正确调用RedisTemplate,必须对其进行一些初始化工作，即主要对它存取的字符串进行一个JSON格式的系列化初始配置
        Jackson2JsonRedisSerializer<Coffee> serializer = new Jackson2JsonRedisSerializer<>(Coffee.class);
        RedisSerializationContext.RedisSerializationContextBuilder<String, Coffee> builder =
                RedisSerializationContext.newSerializationContext(new StringRedisSerializer());
        RedisSerializationContext<String, Coffee> context = builder.value(serializer).build();
        return new ReactiveRedisTemplate<>(factory, context);
    }
}
